package com.netnovelstore.controller;

import com.netnovelstore.annotation.JudgeOfIdentity;
import com.netnovelstore.annotation.NotNullAndBlank;
import com.netnovelstore.annotation.ResultInterceptor;
import com.netnovelstore.annotation.UserNumberByToken;
import com.netnovelstore.dao.UserMapper;
import com.netnovelstore.pojo.Book;
import com.netnovelstore.pojo.Exception.NotDataException;
import com.netnovelstore.pojo.Message;
import com.netnovelstore.pojo.Result;
import com.netnovelstore.service.UserService;
import com.netnovelstore.util.Md5;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

@ResultInterceptor
@RestController
@CrossOrigin
@Api(tags = "更新用户信息接口")
@RequestMapping("/User/Update")
@Slf4j

public class UserUpdateController {

    private UserMapper userMapper;

    private UserService userService;

    @Autowired
    public UserUpdateController(UserMapper userMapper, UserService userService) {
        this.userMapper = userMapper;
        this.userService = userService;
    }

    @JudgeOfIdentity
    @PostMapping("/updatePassword")
    @ApiOperation("修改密码")
    @NotNullAndBlank
    public Result<Boolean> updatePassword(HttpServletRequest request, HttpServletResponse response, @NotBlank String password,@Value("0") @UserNumberByToken Integer userNumber) throws Exception {
        String userName = userMapper.selectUserNameByUserNumber(userNumber);

        if (userName == null || "".equals(userName))
            throw new NotDataException("userName is blank");

        String md5 = Md5.getMD5ByUsernameAndPassword(userName, password);

        if (!userMapper.updatePassword(userNumber, md5)) {
            throw new Exception("update file");
        }

        return Result.ok(true);
    }

    @JudgeOfIdentity
    @PostMapping("/updateUserImg")
    @ApiOperation("修改头像")
    @NotNullAndBlank
    public Result<Boolean> updateUserImg(HttpServletRequest request, HttpServletResponse response, @NotNull String userImg, @Value("0") @UserNumberByToken Integer userNumber) throws Exception {
        if (!userMapper.updateUserImg(userNumber, userImg)) {
            throw new Exception("update file");
        }

        return Result.ok(true);
    }

    @JudgeOfIdentity
    @NotNullAndBlank
    @ResultInterceptor
    @PostMapping("/updateNickName")
    @ApiOperation("修改昵称")
    public Result<Boolean> updateNickName(HttpServletRequest request, HttpServletResponse response, @NotNull String nickName, @Value("0") @UserNumberByToken Integer userNumber) throws Exception {
        log.info("run controller");
        return userService.updateNickName(request, response, userNumber, nickName);
    }

    @PostMapping("/updateBookshelf")
    @ApiOperation("修改书架")
    @NotNullAndBlank
    public Result<Boolean> updateBookshelf(HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String, Book> bookshelf, @Value("0") @UserNumberByToken Integer userNumber) throws Exception {
        if (!userMapper.updateBookshelf(userNumber, bookshelf)) {
            throw new Exception("update file");
        }
        return Result.ok(true);
    }

    @PostMapping("/updateUnread")
    @ApiOperation("修改未读信息")
    @NotNullAndBlank
    public Result<Boolean> updateUnread(HttpServletRequest request, HttpServletResponse response, @RequestBody List<Message> unread, @Value("0") @UserNumberByToken Integer userNumber) throws Exception {

        if (!userMapper.updateUnread(userNumber, unread)) {
            throw new Exception("update file");
        }

        return Result.ok(true);
    }

    @JudgeOfIdentity
    @PostMapping("/updateRead")
    @ApiOperation("修改已读信息")
    @NotNullAndBlank
    public Result<Boolean> updateRead(HttpServletRequest request, HttpServletResponse response, @RequestBody List<Message> read, @Value("0") @UserNumberByToken Integer userNumber) throws Exception {
        if (!userMapper.updateRead(userNumber, read)) {
            throw new Exception("update file");
        }

        return Result.ok(true);
    }

    @JudgeOfIdentity
    @PostMapping("/updateHobby")
    @ApiOperation("修改偏好")
    @NotNullAndBlank
    public Result<Boolean> updateHobby(HttpServletRequest request, HttpServletResponse response, @RequestBody List<String> hobby, @Value("0") @UserNumberByToken Integer userNumber) throws Exception {
        if (!userMapper.updateHobby(userNumber, hobby)) {
            throw new Exception("update file");
        }

        return Result.ok(true);
    }

    @JudgeOfIdentity
    @PostMapping("/updateHistory")
    @ApiOperation("修改浏览历史")
    @NotNullAndBlank
    public Result<Boolean> updateHistory(HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String, Book> history, @Value("0") @UserNumberByToken Integer userNumber) throws Exception {
        if (!userMapper.updateHistory(userNumber, history)) {
            throw new Exception("update file");
        }

        return Result.ok(true);
    }

}
